﻿using System;
using System.Net;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace CollectionInfo {
	public class PhoneConverter : IValueConverter {
		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
			Regex re = new Regex(@"(?<p1>\d{3})\D*(?<p2>\d{3})\D*(?<p3>\d{4})\D*$", RegexOptions.RightToLeft);
			return re.Replace(value + "", "($1) $2-$3");
		}
		public object ConvertBack(object value, Type targetType, object parameter,
													System.Globalization.CultureInfo culture) {
			return value;
		}
	}
	public class StringFormatter : IValueConverter {
		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
			if (!(parameter + "").StartsWith("{")) parameter = "{0:" + parameter + "}";
			return string.Format(parameter + "", value);
		}
		public object ConvertBack(object value, Type targetType, object parameter,
													System.Globalization.CultureInfo culture) {
			return value;
		}
	}
	public class StringParser : IValueConverter {
		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
			var p = parameter + "";
			bool doFormat = p.StartsWith("{}");
			if (doFormat) p = p.Substring(2);
			var re = Regex.Split(p, "//");
			if (re.Length > 1) {
				var m = Regex.Match(value + "", re[1] + "");
				value = m.Groups.Count > 0 ? m.Groups[0].Value : m.Value;
				p = re[0] + "0" + re[2];
			}
			return doFormat ? string.Format(p, value) : value;
		}
		public object ConvertBack(object value, Type targetType, object parameter,
													System.Globalization.CultureInfo culture) {
			return value;
		}
	}
	public class SSNConverter : IValueConverter {
		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
			Regex re = new Regex(@"(?<p1>\d{3})\D*(?<p2>\d{2})\D*(?<p3>\d{4})\D*$", RegexOptions.RightToLeft);
			return re.Replace(value + "", "$1-$2-$3");
		}
		public object ConvertBack(object value, Type targetType, object parameter,
													System.Globalization.CultureInfo culture) {
			return value;
		}
	}
	public class VisibilityConverter : IValueConverter {
		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
			if (value is int) return (int)value <= 0 ? Visibility.Collapsed : Visibility.Visible;
			if (value is bool) return (bool)value ? Visibility.Visible : Visibility.Collapsed;
			throw new Exception("VisibilityConverter does not support " + value.GetType() + " type.");
		}
		public object ConvertBack(object value, Type targetType, object parameter,
													System.Globalization.CultureInfo culture) {
			return value;
		}
	}
	public class BooleanToDoubleConverter : IValueConverter {
		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
			var values = (parameter + "").Split(',');
			return double.Parse((bool)value ? values[1] : values[0]);
		}
		public object ConvertBack(object value, Type targetType, object parameter,
													System.Globalization.CultureInfo culture) {
			return value;
		}
	}
	public class GridLenthBooleanConverter : IValueConverter {
		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
			var values = (parameter + "").Split(',');
			return new System.Windows.Controls.DataGridLengthConverter().ConvertFromString((bool)value ? values[1] : values[0]);
		}
		public object ConvertBack(object value, Type targetType, object parameter,
													System.Globalization.CultureInfo culture) {
			return value;
		}
	}
}
